<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: assignment.php 2870 2010-05-17 03:04:13Z huuphuoc $
 * @since        2.0.5
 */

class Assignment_Models_Dao_Pdo_Mysql_Assignment extends Tomato_Model_Dao
    implements Assignment_Models_Interface_Assignment
{
    public function convert($entity)
    {
        return new Assignment_Models_Assignment($entity);
    }

    public function find($offset, $count, $exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('a' => $this->_prefix . 'assignment'));
        if ($exp) {
            if (isset($exp['enterprise_id'])) {
                $select->where('a.enterprise_id = ?', $exp['enterprise_id']);
            }
            if (isset($exp['producer_account_id']) && isset($exp['assigner_account_id'])) {
                $select->where("a.producer_account_id = {$exp['producer_account_id']} OR a.assigner_account_id = {$exp['assigner_account_id']}");
            }
            if (isset($exp['process_id'])) {
                $select->where($exp['process_id']);
            }
            if (isset($exp['assignment_create_from_date'])) {
                $select->where("a.assignment_create_date >= ?", $exp['assignment_create_from_date']);
            }
            if (isset($exp['assignment_create_to_date'])) {
                $select->where("a.assignment_create_date <= ?", $exp['assignment_create_to_date']);
            }
            if (isset($exp['assignment_due_from_date'])) {
                $select->where("a.assignment_due_date >= ?", $exp['assignment_due_from_date']);
            }
            if (isset($exp['assignment_due_to_date'])) {
                $select->where("a.assignment_due_date <= ?", $exp['assignment_due_to_date']);
            }
            if (isset($exp['assignment_producer'])) {
                $select->where("a.producer_account_id = ?", $exp['assignment_producer']);
            }
            if (isset($exp['order_invoice'])) {
                $select->where("a.order_id = ?", $exp['order_invoice']);
            }
        }
                $select->order('a.assignment_id DESC');
                if(null != $count){
                    $select->limit($count, $offset);
                }
        $rs =   $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function count($exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('a' => $this->_prefix . 'assignment'), array('num_assigment' => 'COUNT(*)'));
        if ($exp) {
            if (isset($exp['enterprise_id'])) {
                $select->where('a.enterprise_id = ?', $exp['enterprise_id']);
            }
            if (isset($exp['producer_account_id']) && isset($exp['assigner_account_id'])) {
                $select->where("a.producer_account_id = {$exp['producer_account_id']} OR a.assigner_account_id = {$exp['assigner_account_id']}");
            }
            if (isset($exp['process_id'])) {
                $select->where($exp['process_id']);
            }
            if (isset($exp['assignment_create_from_date'])) {
                $select->where("a.assignment_create_date >= ?", $exp['assignment_create_from_date']);
            }
            if (isset($exp['assignment_create_to_date'])) {
                $select->where("a.assignment_create_date <= ?", $exp['assignment_create_to_date']);
            }
            if (isset($exp['assignment_due_from_date'])) {
                $select->where("a.assignment_due_date >= ?", $exp['assignment_due_from_date']);
            }
            if (isset($exp['assignment_due_to_date'])) {
                $select->where("a.assignment_due_date <= ?", $exp['assignment_due_to_date']);
            }
            if (isset($exp['assignment_producer'])) {
                $select->where("a.producer_account_id = ?", $exp['assignment_producer']);
            }
            if (isset($exp['order_invoice'])) {
                $select->where("a.order_id = ?", $exp['order_invoice']);
            }
        }
        return $select->query()->fetch()->num_assigment;
    }

    public function getODById($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('od' => $this->_prefix.'shop_order_detail'), array('product_value_type_id_chain'))
                        ->joinInner(array('p' => $this->_prefix.'shop_product'), "od.product_id = p.product_id", array('product_admin_name', 'production_duration'))
                        ->where("od.order_detail_id = ?",$id)
                        ->limit(1);
                        ;
        $rowset = $select->query()->fetchAll();
        $od = new Tomato_Model_RecordSet($rowset, $this);
        return (count($od) == 0) ? null : $od[0];
    }

    public function getById($id){
        $row = $this->_conn
                    ->select()
                    ->from(array('a' => $this->_prefix . 'assignment'))
                    ->where('a.assignment_id = ?', $id)
                    ->limit(1)
                    ->query()
                    ->fetch();
        return (null == $row) ? null : new Assignment_Models_Assignment($row);
    }
	// get process_id with process_type and start_stop =2 (STOP) -> the end process
	public function getPsFinishedByType($type){
		$select = $this->_conn
                        ->select()
                        ->from(array('p' => $this->_prefix.'business_process'))
                        ->joinInner(array('ss' => $this->_prefix.'business_process_step_status'), "p.status_id = ss.id", array('code'))
                        ->where("p.process_type_id = ?",$type)
						->where("p.start_stop = 2")
                        ->limit(1);
                        ;
        $rowset = $select->query()->fetchAll();
        $ps = new Tomato_Model_RecordSet($rowset, $this);
        return (count($ps) == 0) ? null : $ps[0];		
	}
	
    public function getPSById($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('p' => $this->_prefix.'business_process'))
                        ->joinInner(array('ss' => $this->_prefix.'business_process_step_status'), "p.status_id = ss.id", array('code'))
                        ->where("p.process_id = ?",$id)
                        ->limit(1);
                        ;
        $rowset = $select->query()->fetchAll();
        $ps = new Tomato_Model_RecordSet($rowset, $this);
        return (count($ps) == 0) ? null : $ps[0];
    }

    public function getSSById($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('ss' => $this->_prefix.'business_process_step_status'))
                        ->where("ss.id = ?",$id)
                        ->limit(1);
                        ;
        $rowset = $select->query()->fetchAll();
        $ss = new Tomato_Model_RecordSet($rowset, $this);
        return (count($ss) == 0) ? null : $ss[0];
    }

    public function update($assignment){
        return $this->_conn->update($this->_prefix . 'assignment',
                                array(
                                    'process_id'             => $assignment->process_id,
                                    'assignment_notes'       => $assignment->assignment_notes,
                                    'producer_account_id'    => $assignment->producer_account_id,
                                    'assignment_finish_date' => $assignment->assignment_finish_date,
                                ),
                                array(
                                    'assignment_id = ?' => $assignment->assignment_id,
                                ));
    }

    public function getOrderById($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('o' => $this->_prefix.'shop_order'), array('customer_id','customer_notice'))
                        ->where("o.order_invoice = ?",$id)
                        ->limit(1);
                        ;
        $rowset = $select->query()->fetchAll();
        $cus = new Tomato_Model_RecordSet($rowset, $this);
        return (count($cus) == 0) ? null : $cus[0];
    }

    /**public function getSSByType($enterprise_id = null, $type = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('ass' => $this->_prefix . 'business_process_step_status'));
            if(null != $enterprise_id){
                $select->where("ass.enterprise_id = '99999999' OR ass.enterprise_id = $enterprise_id");
            }
            if(null != $type){
                $select->where("ass.step_or_status = ?", $type);
            }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }*/

    public function getRoleByName($name){
        $row = $this->_conn
                    ->select()
                    ->from(array('r' => $this->_prefix . 'core_role'))
                    ->where('r.name = ?', $name)
                    ->limit(1)
                    ->query()
                    ->fetch();
        return (null == $row) ? null : new Core_Models_Role($row);
    }

    public function getPBySId($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('p' => $this->_prefix.'business_process'))
                        ->where("p.status_id = ?",$id)
                        ;
        $rowset = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rowset, $this);
    }

    public function getPByType($type=null){
        $select = $this->_conn
                        ->select()
                        ->from(array('p' => $this->_prefix.'business_process'));
                  if(null != $type){
                    $select->where("p.process_type_id = ?",$type);
                  }
        $rowset = $select->query()->fetchAll();
        return new Tomato_Model_RecordSet($rowset, $this);
    }

    public function add($assignment){
        $this->_conn->insert($this->_prefix . 'assignment',
                            array(
                                'order_id'               => $assignment->order_id,
                                'order_detail_id'        => $assignment->order_detail_id,
                                'producer_account_id'    => $assignment->producer_account_id,
                                'assigner_account_id'    => $assignment->assigner_account_id,
                                'assignment_create_date' => $assignment->assignment_create_date,
                                'assignment_due_date'    => $assignment->assignment_due_date,
                                'assignment_finish_date' => $assignment->assignment_finish_date,
                                'process_id'             => $assignment->process_id,
                                'assignment_notes'       => $assignment->assignment_notes,
                                'enterprise_id'          => $assignment->enterprise_id,
                            ));
        return $this->_conn->lastInsertId($this->_prefix . 'assignment');
    }
}
